package tbalert.web.manager.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import tbalert.hibernate3.Finder;
import tbalert.hibernate3.Updater;
import tbalert.page.Pagination;
import tbalert.web.SysConstants;
import tbalert.web.dao.BaseInfoDao;
import tbalert.web.dao.BlxxkDao;
import tbalert.web.dao.DpsybDao;
import tbalert.web.dao.FxpgbDao;
import tbalert.web.dao.HivbDao;
import tbalert.web.entity.BaseInfo;
import tbalert.web.entity.Blxxk;
import tbalert.web.entity.Dpsyb;
import tbalert.web.entity.Dwdm;
import tbalert.web.entity.Fxpgb;
import tbalert.web.entity.HIVb;
import tbalert.web.manager.BrxxMng;

@Transactional
public class BrxxMngImpl implements BrxxMng {

	@Autowired(required = true)
	private BaseInfoDao baseInfoDao;
	@Autowired(required = true)
	private BlxxkDao blxxkDao;
	@Autowired(required = true)
	private DpsybDao dpsybDao;
	@Autowired(required = true)
	private HivbDao hivbDao;
	@Autowired(required = true)
	private FxpgbDao fxpgbDao;

	@Override
	public BaseInfo save(BaseInfo bean) {
		return baseInfoDao.save(bean);
	}

	@Override
	public Blxxk save(Blxxk bean) {
		return blxxkDao.save(bean);
	}

	@Override
	public Dpsyb save(Dpsyb bean) {
		return dpsybDao.save(bean);
	}

	@Override
	public HIVb save(HIVb bean) {
		return hivbDao.save(bean);
	}

	@Override
	public Fxpgb save(Fxpgb bean) {
		return fxpgbDao.save(bean);
	}

	@Transactional(readOnly = true)
	public String buildKicid(String dwid) {
		String proId = baseInfoDao.getMaxKicid(dwid);
		int i = NumberUtils.toInt(StringUtils.substring(proId, 4));
		String kicidSuffix = String.valueOf(i + 1);
		while (kicidSuffix.length() < 6) {
			kicidSuffix = '0' + kicidSuffix;
		}
		return dwid + kicidSuffix;
	}

	@Override
	public List<BaseInfo> find(Finder finder) {
		return baseInfoDao.findByFinder(finder);
	}

	@Transactional(readOnly = true)
	public Pagination findByFinder(Finder finder, int pageNo, int pageSize) {
		return baseInfoDao.findByFinder(finder, pageNo, pageSize);
	}

	@Transactional(readOnly = true)
	public BaseInfo findByKicid(String kicid) {
		return baseInfoDao.findByKicid(kicid);
	}

	@Override
	public Blxxk findBlxxkById(String kicid) {
		return blxxkDao.findById(kicid);
	}

	@Override
	public Dpsyb findDpsybById(String kicid) {
		return dpsybDao.findById(kicid);
	}

	@Override
	public Fxpgb findFxpgbById(String kicid) {
		return fxpgbDao.findById(kicid);
	}

	@Override
	public HIVb findHIVbById(String kicid) {
		return hivbDao.findById(kicid);
	}

	@Transactional(readOnly = true)
	public boolean kicidExist(String kicid) {
		return findByKicid(kicid) != null;
	}

	@Override
	public BaseInfo changeStatus(String kicid, Boolean status) {
		BaseInfo bean = baseInfoDao.findByKicid(kicid);
		if (bean != null) {
			bean.setSubmit(status);
			Updater<BaseInfo> updater = new Updater<BaseInfo>(bean);
			baseInfoDao.updateByUpdater(updater);
		}
		return bean;
	}

	@Override
	public BaseInfo update(BaseInfo baseInfo) {
		Updater<BaseInfo> updater = new Updater<BaseInfo>(baseInfo);
		return baseInfoDao.updateByUpdater(updater);
	}

	@Override
	public void update(Blxxk blxxk) {
		blxxkDao.update(blxxk);
	}

	@Override
	public Dpsyb update(Dpsyb dpsyb) {
		Updater<Dpsyb> updater = new Updater<Dpsyb>(dpsyb);
		return dpsybDao.updateByUpdater(updater);
	}

	@Override
	public void update(Fxpgb fxpgb) {
		fxpgbDao.update(fxpgb);
	}

	@Override
	public HIVb update(HIVb hivb) {
		Updater<HIVb> updater = new Updater<HIVb>(hivb);
		return hivbDao.updateByUpdater(updater);
	}

	@Override
	public BaseInfo deleteById(String kicid) {
		HIVb hivb = hivbDao.deleteById(kicid);
		Dpsyb dpsyb = dpsybDao.deleteById(kicid);
		Fxpgb fxpgb = fxpgbDao.deleteById(kicid);
		Blxxk blxxk = blxxkDao.deleteById(kicid);
		BaseInfo baseInfo = baseInfoDao.deleteById(kicid);
		baseInfo.setHivb(hivb);
		baseInfo.setDpsyb(dpsyb);
		baseInfo.setBlxxk(blxxk);
		baseInfo.setFxpgb(fxpgb);
		return baseInfo;
	}
	
	public Integer unfinished(String dwid){
		return baseInfoDao.unfinished(dwid);
	}
}
